#include "BlockQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <unistd.h>
#include <ctime>

int myAdd(int x,int y)
{
    return x + y;
}

void* consumer(void* args)//消费者
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)args;
    while(true)
    {
        // int a;
        Task t;
        bq->pop(t);
        // std::cout << "消费一个数据: " << a << std::endl;
        std::cout << pthread_self() <<" consumer: "<< t._x << "+" << t._y << "=" << t() << std::endl;
        // sleep(1);
    }
    return nullptr;
}

void* productor(void* args)//生产者
{
    BlockQueue<Task>* bq = (BlockQueue<Task>*)args;
    // int a = 1;
    while(true)
    {
        int x = rand() % 100;
        int y = rand() % 100;
        Task t(x,y,myAdd);
        bq->push(t);
        // std::cout << "生产一个数据: " << a << std::endl;
        // a++;
        std::cout << pthread_self() <<" productor: "<< t._x << "+" << t._y << "=?" << std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{ 
    srand((uint64_t)time(nullptr) ^  getpid());
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    pthread_t c , p;
    pthread_create(&c,nullptr,consumer,bq);
    pthread_create(&p,nullptr,productor,bq);

    pthread_join(c,nullptr);
    pthread_join(p,nullptr);

    return 0;
}